package com.fastwire.carriere.huawei.mtopsi.ra;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Properties;

import javax.xml.ws.Holder;

import org.tmforum.mtop.fmw.xsd.hdr.v1.CommunicationPatternType;
import org.tmforum.mtop.fmw.xsd.hdr.v1.CommunicationStyleType;
import org.tmforum.mtop.fmw.xsd.hdr.v1.Header;

import com.fastwire.carriere.common.constant.SystemParameterConstants;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.controller.SystemAdminController;
import com.fastwire.carriere.controller.impl.SystemAdminControllerImpl;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.system.vo.SystemParameterVo;

// TODO: Auto-generated Javadoc
/**
 * The Class HuaweiMTOSIConnection.
 */
public class HuaweiMTOSIConnection {

	/** The PROPFILENAME. */
	private static String PROPFILENAME = "credentials.properties";

	/** The Credentials properties. */
	private static Properties CredentialsProperties;

	/** The hostname. */
	private static String hostname;

	/** The hostport. */
	private static String hostport;

	/** The password. */
	private static String password;

	/** The username. */
	private static String username;

	/** The ne fetch size. */
	private static String neFetchSize;

	/** The tl fetch size. */
	private static String tlFetchSize;

	/** The security. */
	private static String security;

	/**
	 * Gets the security.
	 * 
	 * @return the security
	 */
	public static String getSecurity() {
		return security;
	}

	/**
	 * Sets the security.
	 * 
	 * @param security
	 *            the new security
	 */
	public static void setSecurity(String security) {
		HuaweiMTOSIConnection.security = security;
	}

	/**
	 * Gets the hostname.
	 * 
	 * @return the hostname
	 */
	public static String getHostname() {
		return hostname;
	}

	/**
	 * Sets the hostname.
	 * 
	 * @param hostname
	 *            the new hostname
	 */
	public static void setHostname(String hostname) {
		HuaweiMTOSIConnection.hostname = hostname;
	}

	/**
	 * Gets the password.
	 * 
	 * @return the password
	 */
	public static String getPassword() {
		return password;
	}

	/**
	 * Sets the password.
	 * 
	 * @param password
	 *            the new password
	 */
	public static void setPassword(String password) {
		HuaweiMTOSIConnection.password = password;
	}

	/**
	 * Gets the username.
	 * 
	 * @return the username
	 */
	public static String getUsername() {
		return username;
	}

	/**
	 * Sets the username.
	 * 
	 * @param username
	 *            the new username
	 */
	public static void setUsername(String username) {
		HuaweiMTOSIConnection.username = username;
	}

	/**
	 * Gets the credentials properties.
	 * 
	 * @return the credentials properties
	 */
	public static Properties getCredentialsProperties() {
		return CredentialsProperties;
	}

	/**
	 * Sets the credentials properties.
	 * 
	 * @param credentialsProperties
	 *            the new credentials properties
	 */
	public static void setCredentialsProperties(Properties credentialsProperties) {
		CredentialsProperties = credentialsProperties;
	}

	/**
	 * Gets the hostport.
	 * 
	 * @return the hostport
	 */
	public static String getHostport() {
		return hostport;
	}

	/**
	 * Sets the hostport.
	 * 
	 * @param hostport
	 *            the new hostport
	 */
	public static void setHostport(String hostport) {
		HuaweiMTOSIConnection.hostport = hostport;
	}

	/**
	 * Gets the ne fetch size.
	 * 
	 * @return the ne fetch size
	 */
	public static String getNeFetchSize() {
		return neFetchSize;
	}

	/**
	 * Sets the ne fetch size.
	 * 
	 * @param neFetchSize
	 *            the new ne fetch size
	 */
	public static void setNeFetchSize(String neFetchSize) {
		HuaweiMTOSIConnection.neFetchSize = neFetchSize;
	}

	/**
	 * Gets the tl fetch size.
	 * 
	 * @return the tl fetch size
	 */
	public static String getTlFetchSize() {
		return tlFetchSize;
	}

	/**
	 * Sets the tl fetch size.
	 * 
	 * @param tlFetchSize
	 *            the new tl fetch size
	 */
	public static void setTlFetchSize(String tlFetchSize) {
		HuaweiMTOSIConnection.tlFetchSize = tlFetchSize;
	}

	static {

		try {
			InputStream propsIn = HuaweiMTOSIConnection.class.getClassLoader()
					.getResourceAsStream(PROPFILENAME);

			CredentialsProperties = new Properties();
			CredentialsProperties.load(propsIn);

			hostname = CredentialsProperties.getProperty("hostname");
			hostport = CredentialsProperties.getProperty("hostport");
			password = CredentialsProperties.getProperty("password");
			username = CredentialsProperties.getProperty("username");
			security = CredentialsProperties.getProperty("security");
			neFetchSize = CredentialsProperties.getProperty("fetchsize_ne");
			tlFetchSize = CredentialsProperties.getProperty("fetchsize_tl");

			propsIn.close();

		} catch (IOException ioExp) {
			CommonLogger.warn(HuaweiMTOSIConnection.class,
					"Cannot load properties file" + PROPFILENAME);
		} catch (Exception e) {
			e.printStackTrace();
			CommonLogger
					.warn(HuaweiMTOSIConnection.class,
							"Exception while getting values from properties file. So leaving as default");
		}

	}

	/**
	 * Gets the service url.
	 * 
	 * @param serviceName
	 *            the service name
	 * @return the service url
	 * @throws DAOException
	 */
	public static String getServiceURL(String serviceName) throws DAOException {

		SystemAdminController controller = new SystemAdminControllerImpl();
		List<SystemParameterVo> parameters = controller
				.getSystemParametersbyCatelog(SystemParameterConstants.U2000_PARAMTER_PRIMARYIPADDRESS);
		for (SystemParameterVo parameter : parameters) {
			if (SystemParameterConstants.U2000_PARAMTER_PRIMARYIPADDRESS
					.equals(parameter.getName())) {
				hostname = parameter.getValue();
			} else if (SystemParameterConstants.U2000_PARAMETER_PORT
					.equals(parameter.getName())) {
				hostport = parameter.getValue();
			} else if (SystemParameterConstants.U2000_PARAMTER_SECURITY
					.equals(parameter.getName())) {
				security = parameter.getValue();
			}
		}
		return "http://" + hostname + ":" + hostport + "/" + serviceName;
	}

	/**
	 * Gets the web service url.
	 * 
	 * @param serviceName
	 *            the service name
	 * @return the web service url
	 * @throws MalformedURLException
	 *             the malformed url exception
	 */
	public static URL getWebServiceURL(String serviceName)
			throws MalformedURLException {

		return new URL("http://" + hostname + ":" + hostport + "/"
				+ serviceName + "?wsdl");

	}

	/**
	 * Gets the single response header.
	 * 
	 * @return the single response header
	 */
	public static Holder<Header> getSingleResponseHeader() {
		Holder<Header> headerHolder = new Holder<Header>();
		Header header = new Header();
		headerHolder.value = header;
		header.setSecurity(getSecurity());
		header.setCommunicationPattern(CommunicationPatternType.SIMPLE_RESPONSE);
		header.setCommunicationStyle(CommunicationStyleType.RPC);
		return headerHolder;
	}

}
